import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QPushButton,
    QMessageBox,
    QLabel,
    QCheckBox,
)

from PyQt5.QtGui import QPixmap


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        self.setGeometry(300, 300, 330, 300)
        self.setWindowTitle("消息对话框")
        self.la = QLabel("这里将会显示我们选择的按钮信息", self)
        self.la.move(20, 20)

        self.bt1 = QPushButton("提示", self)
        self.bt1.move(20, 70)

        self.bt2 = QPushButton("询问", self)
        self.bt2.move(120, 70)

        self.bt3 = QPushButton("警告", self)
        self.bt3.move(220, 70)

        self.bt4 = QPushButton("错误", self)
        self.bt4.move(20, 140)

        self.bt5 = QPushButton("关于", self)
        self.bt5.move(120, 140)
        self.bt6 = QPushButton("关于Qt", self)
        self.bt6.move(220, 140)

        self.bt1.clicked.connect(self.info)
        self.bt2.clicked.connect(self.question)
        self.bt3.clicked.connect(self.warning)
        self.bt4.clicked.connect(self.critical)
        self.bt5.clicked.connect(self.about)
        self.bt6.clicked.connect(self.aboutqt)

        self.show()

    def info(self):
        reply = QMessageBox.information(
            self,
            "提示",
            "这是一个信息提示对话框",
            QMessageBox.Ok | QMessageBox.Close,
            QMessageBox.Close,
        )
        if reply == QMessageBox.Ok:
            self.la.setText("你选择了ok!")
        else:
            self.la.setText("你选择了Close!")

    def question(self):
        reply = QMessageBox.question(
            self,
            "询问",
            "这是一个消息提示对话框！",
            QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
            QMessageBox.Cancel,
        )
        if reply == QMessageBox.Yes:
            self.la.setText("你选择了Yes!")
        elif reply == QMessageBox.No:
            self.la.setText("你选择了No!")
        else:
            self.la.setText("你选择了Cancel!")

    def warning(self):
        # reply = QMessageBox.warning(self,'警告','这是一个警告消息对话框', QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel, QMessageBox.Save)
        msgBox = QMessageBox()
        msgBox.setWindowTitle("警告")
        msgBox.setIcon(QMessageBox.Warning)
        msgBox.setText("这是一个警告消息对话框")
        msgBox.setInformativeText("出现更改愿意保存吗?")
        Save = msgBox.addButton("保存", QMessageBox.AcceptRole)
        NoSave = msgBox.addButton("取消", QMessageBox.RejectRole)
        Cancel = msgBox.addButton("不保存", QMessageBox.DestructiveRole)
        msgBox.setDefaultButton(Save)

        cb = QCheckBox("所有文档都按此操作")
        msgBox.setCheckBox(cb)

        cb.stateChanged.connect(self.check)

        reply = msgBox.exec()

        if reply == QMessageBox.AcceptRole:
            self.la.setText("你选择了保存！")
        elif reply == QMessageBox.RejectRole:
            self.la.setText("你选择了取消！")
        else:
            self.la.setText("你选择了不保存！")

    def critical(self):
        # reply = QMessageBox.critical(self,'错误','这是一个错误消息对话框', QMessageBox.Retry | QMessageBox.Abort | QMessageBox.Ignore , QMessageBox.Retry)
        msgBox = QMessageBox()
        msgBox.setIcon(QMessageBox.Critical)
        msgBox.setWindowTitle("错误")
        msgBox.setText("这是一个错误消息对话框")
        msgBox.setStandardButtons(
            QMessageBox.Retry | QMessageBox.Abort | QMessageBox.Ignore
        )
        msgBox.setDefaultButton(QMessageBox.Ignore)
        msgBox.setDetailedText("这是详细的信息：学点编程吧，我爱你！")

        reply = msgBox.exec()

        if reply == QMessageBox.Retry:
            self.la.setText("你选择了Retry！")
        elif reply == QMessageBox.Abort:
            self.la.setText("你选择了Abort！")
        else:
            self.la.setText("你选择了Ignore！")

    def about(self):
        # QMessageBox.about(self,'关于','这是一个关于消息对话框!')
        msgBox = QMessageBox(QMessageBox.NoIcon, "关于", "不要意淫了，早点洗洗睡吧!")
        msgBox.setIconPixmap(QPixmap("beauty.png"))
        msgBox.exec()

    def aboutqt(self):
        QMessageBox.aboutQt(self, "关于Qt")

    def check(self):
        if self.sender().isChecked():
            self.la.setText("你打勾了哦")
        else:
            self.la.setText("怎么又不打了啊")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
